package wjl.project.dubbotestshop;

import com.alibaba.fastjson.JSON;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;

import javax.annotation.Resource;
import java.util.Date;
import java.util.UUID;

/**
 * 不需要异步  发送到队列 本来就可以直接返回
 * */

@Component
@Slf4j
public class KafkaSender {

    @Resource
    private KafkaTemplate<String, String> kafkaTemplate;

    public ListenableFuture
            <SendResult<String, String>> BuyGoodsSync(Goods goods) {
        return  kafkaTemplate.send("goods", JSON.toJSONString(goods));
    }
    public boolean BuyGoods(Goods goods) {
        try {
            kafkaTemplate.send("goods", JSON.toJSONString(goods));
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return  false;
        }
    }
}
